package com.yxp423.ssmempl.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxp423.ssmempl.bean.Travel;
import com.yxp423.ssmempl.bean.User;
import com.yxp423.ssmempl.mapper.TravelMapper;
import com.yxp423.ssmempl.service.ITravelService;
import com.yxp423.ssmempl.utils.base.AjaxResult;
import com.yxp423.ssmempl.utils.consts.ApplyConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @Description: java类作用描述
 * @Author: yxp
 * @CreateDate: 2019-05-04 12:23
 * @Version: 1.0
 */
@Service
public class TravelServiceImpl implements ITravelService {

    @Autowired
    private TravelMapper travelMapper;


    IService<Travel> mpService =  new ServiceImpl<TravelMapper,Travel>();

    @Override
    public int saveTravel(Travel travel) {
        int flag = -1;

//        flag = travelMapper.savetravel(travel);
        flag = travelMapper.insert(travel);
        return flag;
    }

    @Override
    public IPage<Travel> getMyTravel(Page page, User user) {

        QueryWrapper<Travel> condition = new QueryWrapper<Travel>();

        travelMapper.selectPage(
                page,
                condition.eq("user_id", user.getUserId()).orderByAsc("review_status")
        );

        return page;
    }

    @Override
    public IPage<Travel> getMyReviewTravel(Page page, User user) {

        QueryWrapper<Travel> conditions = new QueryWrapper<>();

        travelMapper.selectPage(page,
                conditions
                        .eq("reviewer_id",user.getUserId())
                        .eq("review_status", ApplyConst.STATUS_CHECKING)
                        .orderByDesc("creat_time"));

        return page;
    }

    @Override
    public AjaxResult updateTravel(Travel travel) {

        AjaxResult result = new AjaxResult();


        Travel travelById = travelMapper.selectById(travel.getApplyId());

        if (ObjectUtil.isNull(travelById)){

            result.setSuccess(false);
            result.setMsg("该日志不存在");
            return result;

        }

        travel.setReviewTime(DateUtil.date());

        int flag = travelMapper.updateById(travel);
        if (flag > 0){
            result.setSuccess(true);
            result.setMsg("审核成功");

        }else{
            result.setSuccess(false);
            result.setMsg("审核失败");
        }
        return result;
    }

    @Override
    public AjaxResult updateBatch(Travel travel, List<Travel> travelList) {

        boolean flag = mpService.updateBatchById(travelList, travelList.size());

        return null;
    }


}
